//
//  View+Extension.swift
//  Test
//
//  Created by zhaojunming on 2024/8/30.
//

import SwiftUI

let kWidth = UIScreen.main.bounds.size.width
let kHeight = UIScreen.main.bounds.size.height

extension View {
    func linerShow(_ isAnimating: Bool, index: Int) -> some View {
        self
            .offset(y: isAnimating ? 0 : 1000)
            .animation(.spring(response: 0.6, dampingFraction: 0.8, blendDuration: 0.8).delay(Double(index) * 0.1), value: isAnimating)
    }
    
    func flipShow(_ isAnimating: Bool) -> some View {
        self
            .rotation3DEffect(.degrees(isAnimating ? 90 : -90), axis: (x: 0.0, y: 1.0, z: 0.0))
            .animation(.spring(response: 0.6, dampingFraction: 0.8, blendDuration: 0.8), value: isAnimating)
    }
    
    @ViewBuilder
    func configCard() -> some View {
        self
            .frame(width: (kWidth - 100)/2, height: (kWidth - 60)/2 * 1.1)
            .padding(10)
            .background(.white)
            .clipShape(RoundedRectangle(cornerRadius: 14))
            .shadow(color: .black.opacity(0.1), radius: 6)
    }
}

extension View {
    func defaultShadow(radius: CGFloat = 5) -> some View {
        self.shadow(color: Color.black.opacity(0.1), radius: radius, x: 0, y: 0)
    }
    
    func dcChange<V>(of value: V, _ action: @escaping (_ oldValue: V, _ newValue: V) -> Void) -> some View where V : Equatable {
        if #available(iOS 17.0, *) {
            return self.onChange(of: value, action)
        } else {
            return self.onChange(of: value) { action(value, $0) }
        }
    }
}
